import axios from 'axios'
import store from '../store'

axios.defaults.baseURL = 'http://localhost:3001'

// 请求
axios.interceptors.request.use(config => {
  store.commit('setLoading', true)
  store.commit('setError', { status: false, message: '' })
  return config
})

// interceptors 响应的拦截器
// 响应
axios.interceptors.response.use(resp => {
//   test loading效果
  setTimeout(() => {
    store.commit('setLoading', false)
  }, 2000)
  store.commit('setLoading', false)
  return resp
}, e => {
  // console.log(e.response)
  const { error } = e.response.data
  store.commit('setError', { status: true, message: error })
  store.commit('setLoading', false)
  return Promise.reject(error)
})

export default axios
